package com.dsl.tms.repository;

import com.dsl.tms.entity.db.Db;
import com.dsl.tms.entity.request.DbSourse;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

public interface DataSourceRepository extends PagingAndSortingRepository<Db,Integer> {

    @Query(value = " SELECT db FROM Db db WHERE db.dbName Like CONCAT('%',:#{#dbSource.dbName},'%') " +
            " AND db.driverClassName LIKE concat('%',:#{#dbSource.driverClassName},'%') " +
            " AND db.url LIKE CONCAT('%',:#{#dbSource.url},'%') " +
            " AND db.ofGroup LIKE CONCAT('%',:#{#dbSource.ofGroup},'%')" +
            " AND db.username LIKE concat('%',:#{#dbSource.username},'%') ")
    public Page<Db> queryAllWithKeywords(@Param("dbSource") DbSourse dbSource, Pageable pageable);

    @Modifying
    @Query(" UPDATE Db db SET db.dbName = :#{#dbSource.dbName},db.driverClassName = :#{#dbSource.driverClassName}," +
            " db.ofGroup = :#{#dbSource.ofGroup}, db.url = :#{#dbSource.url}," +
            " db.username = :#{#dbSource.username}, db.password = :#{#dbSource.password} " +
            " WHERE db.id = :#{#dbSource.id}")
    public void updateOne(@Param("dbSource") Db dbSource);



}
